package com.airS.task.fileMgr.utils.mvc;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * MVC线程变量
 * Created by alan on 2017/3/14.
 */
public class MVCShareValue {

	private static ThreadLocal<Map<String, Object>>	threadLocal	= new ThreadLocal<Map<String, Object>>();

	public static void initValue(HttpServletRequest request, HttpServletResponse response) {
		clear();
		Map<String, Object> map = new HashMap<String, Object>(2);
		map.put("request", request);
		map.put("response", response);
		threadLocal.set(map);
	}

	public static void clear() {
		if (threadLocal != null) {
			threadLocal.remove();
		}
	}

	public static HttpServletRequest getRequest() {
		if (threadLocal == null || threadLocal.get() == null) {
			return null;
		}
		return (HttpServletRequest) threadLocal.get().get("request");
	}

	public static HttpServletResponse getResponse() {
		if (threadLocal == null || threadLocal.get() == null) {
			return null;
		}
		return (HttpServletResponse) threadLocal.get().get("response");
	}

	public static HttpSession getSession() {
		HttpServletRequest req = getRequest();
		return req == null ? null : req.getSession();
	}

}
